# !/user/bin/env python3
# -*- coding:utf-8 -*-
import pytest
from selenium import webdriver
import unittest, time
from contrast import ImageCompare
from ddt import ddt,data
from public import *
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import shutil
import datetime
import logging
from logger import *
import os
from PIL import ImageGrab
import requests

# class TestAutomation:
import sys
sys.setrecursionlimit(10000)

def set_sum(i):
    sum = i
    return sum


# if __name__ == '__main__':
#     for i in range(2):
#         print(i)
#         a = set_sum(i)
#         print(a)






def login_():
    driver = webdriver.Chrome()
    driver.get('http://127.0.0.1:8189/#/')
    driver.maximize_window()
    driver.find_element_by_xpath('//input[@class="yg-input__inner" and @placeholder="请输入手机号"]').send_keys("18607380974")  # 输入手机号
    driver.find_element_by_xpath('//input[@class="yg-input__inner" and @placeholder="请输入密码"]').send_keys('111111')  # 输入密码
    driver.find_element_by_xpath('//div[@class="zh_postForm"]').click()  # 点击登录
    return driver

def test_01_CT_shooting(i,c):
    logging.info('CT开始时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
    logging.info('test_01_CT_shooting第{}次'.format(i))
    sum = set_sum(i)  #判断 获取当前次数
    logging.info(sum)
    if sum != 0:
        if  c:
            driver = c
            logging.info('test_01_CT_shooting_第{}次'.format(i))
            time.sleep(5)
            driver.find_element_by_xpath('//div[text()="登记"]').click()  # 进入登记页
            time.sleep(1)
            patient_name = random_name()
            driver.find_element_by_xpath('//input[@placeholder="患者姓名" and @class="yg-input__inner" ]').send_keys("{}".format(patient_name))  # 输入患者姓名参数化
            time.sleep(1)
            driver.find_element_by_xpath('//div[@class="patients curpo clickImg"]').click()  # 选择拍摄模式
            driver.find_element_by_xpath('//div[@class="check-in-wrap"]/following-sibling::div[1]//div[2]').click()  # 点击下一步
            time.sleep(1)
            driver.find_element_by_xpath('//button[@class="ok-btn userSelect bianseBorder"]').click()  # 点击确认

            try:
                progress_locator = (By.XPATH, '//div[contains(text()," 准备完成")]')  # 等待准备完成元素可见
                WebDriverWait(driver, 90, 1).until(EC.visibility_of_element_located(progress_locator))
                time.sleep(1)
                driver.find_element_by_xpath('//div[@class="yg-imgtxtbtn text-center"]').click()  # 再点击下一步

                shutil.copy('D:\\HandSwitchOn.txt', 'D:\\CTDebug')  # 复制手闸文件到CTDebug目录
                time.sleep(100)  # 等待采集曝光完成
                filepath = 'E:\\CTscreenshot\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
                driver.save_screenshot(filepath)
                time.sleep(3)
                # driver.find_element_by_xpath('//div[@class="yg-button-abs2 buttonicon-div"]/div').click()
            # except:
            #     error_filepath = 'E:\\CTscreenshot\\error\\{}.png'.format(
            #         str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
            #     driver.save_screenshot(error_filepath)
            #     logging.error("CT{}进度卡住！！！".format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
            finally:
                driver.find_element_by_xpath('//i[@class="yg-icon-close"]').click()  # CT叉掉元素

                time.sleep(5)
                driver.find_element_by_xpath('//div[text()="已检查"]').click()  # 已检查元素
                try:
                    loc_upload = (By.XPATH, '//div[contains(text(),"已上传")]')
                    if WebDriverWait(driver, 150, 1).until(EC.visibility_of_all_elements_located(loc_upload)):
                        logging.info('上传成功')
                    else:
                        logging.info('上传失败')
                except:
                    logging.info('上传失败')
            logging.info('CT开始时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))

    else:
        driver = login_()
        time.sleep(5)
        driver.find_element_by_xpath('//div[text()="登记"]').click()  # 进入登记页
        time.sleep(1)
        patient_name = random_name()
        driver.find_element_by_xpath('//input[@placeholder="患者姓名" and @class="yg-input__inner" ]').send_keys("{}".format(patient_name))  # 输入患者姓名参数化
        time.sleep(1)
        driver.find_element_by_xpath('//div[@class="patients curpo clickImg"]').click() #选择拍摄模式
        driver.find_element_by_xpath('//div[@class="check-in-wrap"]/following-sibling::div[1]//div[2]').click()  # 点击下一步
        time.sleep(1)
        driver.find_element_by_xpath('//button[@class="ok-btn userSelect bianseBorder"]').click()  #点击确认
        try:
            progress_locator = (By.XPATH, '//div[contains(text()," 准备完成")]') #等待准备完成元素可见
            WebDriverWait(driver,90,1).until(EC.visibility_of_element_located(progress_locator))
            time.sleep(1)
            driver.find_element_by_xpath('//div[@class="yg-imgtxtbtn text-center"]').click()  # 再点击下一步

            shutil.copy('D:\\HandSwitchOn.txt', 'D:\\CTDebug')  # 复制手闸文件到CTDebug目录
            time.sleep(100)  # 等待采集曝光完成
            filepath = 'E:\\CTscreenshot\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
            driver.save_screenshot(filepath)
            driver.find_element_by_xpath('//div[@class="yg-button-abs2 buttonicon-div"]/div').click()
        except:
            error_filepath = 'E:\\CTscreenshot\\error\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
            driver.save_screenshot(error_filepath)
            logging.error("CT{}进度卡住！！！".format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        finally:
            driver.find_element_by_xpath('//i[@class="yg-icon-close"]').click()  #CT叉掉元素

            time.sleep(5)
            driver.find_element_by_xpath('//div[text()="已检查"]').click()  #已检查元素
            try:
                loc_upload =(By.XPATH,'//div[contains(text(),"已上传")]')
                if WebDriverWait(driver,150,1).until(EC.visibility_of_all_elements_located(loc_upload)):
                    logging.info('上传成功')
                else:
                    logging.info('上传失败')
            except:
                logging.info('上传失败')
        logging.info('CT开始时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))

    return driver

def test_02_Panoramic_shooting(i,a):
    # sum = set_sum(i)
    # if sum ==0:
    driver2 = a
    logging.info('PX开始时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
    logging.info('PX第{}次'.format(i))
    time.sleep(3)
    driver2.find_element_by_xpath('//div[text()="登记"]').click()
    time.sleep(1)
    patient_name2 = random_name()
    driver2.find_element_by_xpath('//input[@placeholder="患者姓名" and @class="yg-input__inner" ]').send_keys("{}".format(patient_name2))  # 输入患者姓名参数化
    driver2.find_element_by_xpath('//div[@class="patients curpo clickImg"]/following-sibling::div[1]').click()
    driver2.find_element_by_xpath('//div[@class="check-in-wrap"]/following-sibling::div[1]//div[2]').click()   # 选择模式后的下一步
    time.sleep(3)
    driver2.find_element_by_xpath('//button[@class="ok-btn bianseBorder"]').click()
    try:
        # time.sleep(60)
        progress_locator = (By.XPATH, '//div[contains(text()," 准备完成")]')  #等待准备完成元素可见
        WebDriverWait(driver2,90,1).until(EC.visibility_of_element_located(progress_locator))
        time.sleep(1)
        driver2.find_element_by_xpath('//div[@class="yg-imgtxtbtn text-center"]').click()
        time.sleep(1)
        driver2.find_element_by_xpath('//div[@class="yg-button-abs1"]//div[3]').click()
        time.sleep(1)
        shutil.copy('D:\\HandSwitchOn.txt','D:\\CTDebug')
        time.sleep(60)
        filepath = 'E:\\PXscreenshot\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
        driver2.save_screenshot(filepath)
        driver2.find_element_by_xpath('//div[@class="yg-button-abs2 buttonicon-div"]/div').click()
    except:
        error_filepath = 'E:\\PXscreenshot\\error\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
        driver2.save_screenshot(error_filepath)
        logging.error("PX{}进度卡住！！！".format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
    finally:

        driver2.find_element_by_xpath('//div[@class="yg-button-abs2 buttonicon-div"]').click()  #PX勾选元素
        time.sleep(5)
        driver2.find_element_by_xpath('//div[text()="已检查"]').click()  # 已检查元素
        try:
            loc_upload = (By.XPATH, '//div[contains(text(),"已上传")]')
            if WebDriverWait(driver2,30, 1).until(EC.visibility_of_all_elements_located(loc_upload)):
                logging.info('上传成功')
            else:
                logging.info('上传失败')
        except:
            logging.info('上传失败')
    logging.info('PX结束时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))

    return driver2
    # 头侧拍摄
    #
def test_03_Head_side_shot(i,b):
    drive3 = b
    logging.info('DX开始时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
    logging.info('DX第{}次'.format(i))
    time.sleep(3)
    drive3.find_element_by_xpath('//div[text()="登记"]').click()
    time.sleep(1)
    patient_name3 = random_name()
    drive3.find_element_by_xpath('//input[@placeholder="患者姓名" and @class="yg-input__inner" ]').send_keys("{}".format(patient_name3)) # 输入患者姓名参数化
    drive3.find_element_by_xpath('//div[@class="patients curpo clickImg"]/following-sibling::div[2]').click()
    drive3.find_element_by_xpath('//div[@class="check-in-wrap"]/following-sibling::div[1]//div[2]').click()  #选择模式下的下一步
    time.sleep(3)
    drive3.find_element_by_xpath('//button[@class="ok-btn m-b40 m-t205 bianseBorder"]').click() #确定按钮
    try:
        progress_locator = (By.XPATH, '//div[@class="middle-div"]/div[4]')  #等待准备完成元素可见
        WebDriverWait(drive3,90,1).until(EC.visibility_of_element_located(progress_locator))
        time.sleep(1)
        drive3.find_element_by_xpath('//div[@class="yg-imgtxtbtn text-center"]').click()
        time.sleep(1)
        shutil.copy('D:\\HandSwitchOn.txt','D:\\CTDebug')
        time.sleep(80)
        filepath = 'E:\\DXscreenshot\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
        drive3.save_screenshot(filepath)
        drive3.find_element_by_xpath('//div[@class="buttonicon-right buttonicon-div"]').click()

    except:
        error_filepath = 'E:\\DXscreenshot\\error\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
        drive3.save_screenshot(error_filepath)
        logging.error("DX{}进度卡住！！！".format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
    finally:
        logging.info('DX结束时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        drive3.find_element_by_xpath('//div[@class="buttonicon-right buttonicon-div"]').click()  #DX勾选元素
        time.sleep(5)
        drive3.find_element_by_xpath('//div[text()="已检查"]').click()  # 已检查元素
        try:
            loc_upload = (By.XPATH, '//div[contains(text(),"已上传")]')
            if WebDriverWait(drive3, 30, 1).until(EC.visibility_of_all_elements_located(loc_upload)):
                logging.info('上传成功')
            else:
                logging.info('上传失败')
        except:
            logging.info('上传失败')
            # self.driver.quit()
    return drive3



if __name__ == '__main__':
    c = login_()
    for i in range(100):
        # logging.info(i)
        # TestAutomation().test_01_CT_shooting()
        # time.sleep(1)
        # TestAutomation().test_02_Panoramic_shooting()
        # time.sleep(1)
        # TestAutomation().test_03_Head_side_shot()
        # time.sleep(1)
        c = test_01_CT_shooting(i+1,c)
        c = test_02_Panoramic_shooting(i+1,c)
        c = test_03_Head_side_shot(i+1,c)